import { getBase64ByUrl } from "@/common/utils";
// 按钮点击下载
export default (app: any) => {
  app.directive('download', (el: any, e: any) => {
    el.onclick = () => {
      const url = e.value?.url || '';
      if (!url) {
        return;
      }
      switch (e.value?.type) {
        case 'image':
          downloadImage(url);
          break;
      }
    }
  })
};
// 下载图片
const downloadImage = async (url: string) => {
  const arr = url.split('/');
  const fileName = arr[arr.length - 1];
  const linkNode = document.createElement("a");
  linkNode.download = fileName;
  linkNode.style.display = "none";
  let dataUrl:string = getBase64ByUrl(url) as any;
  linkNode.href = dataUrl;
  document.body.appendChild(linkNode);
  linkNode.click();
  URL.revokeObjectURL(linkNode.href);
  document.body.removeChild(linkNode);
}
